package www.worq.controller.servlets;

import java.io.IOException;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import www.worq.controller.classes.EmailUtility;


/**
 * Servlet para envio de correo 
 * 
 * @author www.blancosMikel
 * 
 */
@SuppressWarnings("serial")
@WebServlet(description = "Valida la sesion", urlPatterns = { "/email" })
public class emailSVR extends HttpServlet {
	private String host;
	private String port;
	private String user;
	private String pass;

	public emailSVR(){
		super();
		System.out.println("SVR.EmailSendingServlet----------> Ejecutandose!");
	}
	
	public void init() {
		// reads SMTP server setting from web.xml file
		ServletContext context = getServletContext();
		host = context.getInitParameter("host");
		port = context.getInitParameter("port");
		user = context.getInitParameter("user");
		pass = context.getInitParameter("pass");
	}

	protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		System.out.println("SVR.EmailSendingServlet----------> doPost");
		// reads form fields
		String recipient = request.getParameter("mail");
		String subject = request.getParameter("nombre");
		String telefono = request.getParameter("telefono");
		String content = request.getParameter("comentario");
		StringBuilder comentario = new StringBuilder();

		comentario.append("Nombre: ");
		comentario.append(subject);
		comentario.append("\n");
		comentario.append("Correo: ");
		comentario.append(recipient);
		comentario.append("\n");
		comentario.append("Telefono: ");
		comentario.append(telefono);
		comentario.append("\n");
		comentario.append("Comento:\n");
		comentario.append(content);
		String resultMessage = "";
		
		try {			
			EmailUtility.sendEmail(host, port, user, pass, user,"Comentarios Pagina WEB", comentario.toString());
			resultMessage = "Email enviado satisfactoriamente";
			System.out.println("SVR.EmailSendingServlet.doPost----------> Email enviado satisfactoriamente");
		} catch (Exception ex) {
			ex.printStackTrace();
			resultMessage = "Se ha producido un error: " + ex.getMessage();
			System.out.println("SVR.EmailSendingServlet.doPost----------> Se ha producido un error" + ex.getMessage());
		} finally {			
			// enviando respuestas
			request.setAttribute("Message", resultMessage);
			getServletContext().getRequestDispatcher("/VIEW/COMMONS/contacto.jsp").forward(request, response);
			System.out.println("SVR.EmailSendingServlet.doPost----------> forward contacto.jsp!");
		}
	}
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		System.out.println("SVR.EmailSendingServlet----------> doGet");
		response.sendRedirect("worqControl");
	}
}